package com.hyh.shiyanshiguanli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.shiyanshiguanli.Expections.NullException;
import com.hyh.shiyanshiguanli.constant.MessageConstant;
import com.hyh.shiyanshiguanli.domain.dto.*;
import com.hyh.shiyanshiguanli.domain.entity.Classify;
import com.hyh.shiyanshiguanli.domain.entity.LibInformation;
import com.hyh.shiyanshiguanli.domain.entity.LibManager;
import com.hyh.shiyanshiguanli.domain.result.PageResult;
import com.hyh.shiyanshiguanli.mapper.ClassifyMapper;
import com.hyh.shiyanshiguanli.mapper.LibInformationMapper;
import com.hyh.shiyanshiguanli.mapper.LibManagerMapper;
import com.hyh.shiyanshiguanli.mapper.LibMapper;
import com.hyh.shiyanshiguanli.service.ClassifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Autowired
    ClassifyMapper classifyMapper;

    @Autowired
    LibManagerMapper libManagerMapper;

    @Autowired
    LibInformationMapper libInformationMapper;

    @Override
    public PageResult page(ClassifyDTO classifyDTO) {
        PageHelper.startPage(classifyDTO.getPage(),classifyDTO.getPageSize());
        Page<Classify> classifies = classifyMapper.page(classifyDTO);
        long total = classifies.getTotal();
        List<Classify> result = classifies.getResult();

        PageResult result1 = PageResult.builder()
                .recodes(result)
                .total(total)
                .build();
        return result1;
    }

    @Override
    public Classify selectById(Integer id) {
        Classify classify = classifyMapper.selectById(id);
        return classify;
    }

    /**
     * 根据id更新
     * @param classifyUpdateDTO
     */

    @Transactional
    @Override
    public void updateById(ClassifyUpdateDTO classifyUpdateDTO) {
        Classify classify = new Classify();
        BeanUtils.copyProperties(classifyUpdateDTO, classify);
        LibManager libManager = libManagerMapper.selectById(classifyUpdateDTO.getLibManagerId());
        String name = libManager.getName();
        classify.setLibManager(name);
        classifyMapper.updateById(classify);

        LibUpdateByClassifyDTO libUpdateByClassifyDTO = new LibUpdateByClassifyDTO();
        libUpdateByClassifyDTO.setLibManagerId(classify.getLibManagerId());
        libUpdateByClassifyDTO.setLibManager(classify.getLibManager());
        libUpdateByClassifyDTO.setLibClassifyId(classify.getId());
        libUpdateByClassifyDTO.setLibClassify(classify.getName());

        libInformationMapper.updateByLibManagerId(libUpdateByClassifyDTO);

    }
    /**
     * 添加
     * @param classifyAddDTO
     */
    @Override
    public void add(ClassifyAddDTO classifyAddDTO) {
        if (classifyAddDTO.getLibManagerId() == null){
            throw new NullException(MessageConstant.NULL);
        }
        Classify classify = new Classify();
        BeanUtils.copyProperties(classifyAddDTO,classify);
        LibManager libManager = libManagerMapper.selectById(classifyAddDTO.getLibManagerId());
        String name = libManager.getName();
        classify.setLibManager(name);
        classifyMapper.add(classify);




    }

    @Override
    public int deleteById(Integer id) {
        classifyMapper.deleteById(id);
        return MessageConstant.SUCCESS;
    }

    /**
     * 查询所有实验室管理员
     * @return
     */
    @Override
    public List<LibManager> selectManager() {
        List<LibManager> LibManagers = libManagerMapper.select();

        return LibManagers;
    }


}
